

from django.http import HttpResponse
from django.forms.models import model_to_dict
from django.http import JsonResponse
from mChatAI import ai_api,env_conf
import json
from django.views.decorators.csrf import csrf_exempt
from datetime import datetime

from mChatAI.env_conf import basicprompt

#同时向多个模型提问
def chat(request):
    if request.method == 'POST':
        data = json.loads(request.body)
        prompt = data.get('prompt')
        sysprompt = data.get('sysprompt')
        print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"),prompt)

        res = ai_api.run(sysprompt,prompt)
        return JsonResponse(res, safe=False)

#单模型提问
def chat_qwen3_coder(request):
    conf = env_conf.qwen3_coder
    if request.method == 'POST':
        data = json.loads(request.body)
        prompt = data.get('prompt')
        sysprompt = data.get('sysprompt')
        print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"),prompt)

        res = ai_api.ai_model(conf[0], conf[1], conf[2], sysprompt,prompt).chat_api()
        res = {"response": f'<span style="color: green;">{conf[0]}</span>'+'\n'+res}
        return JsonResponse(res, safe=False)

def chat_deepseek(request):
    conf = env_conf.deepseek
    if request.method == 'POST':
        data = json.loads(request.body)
        prompt = data.get('prompt')
        sysprompt = data.get('sysprompt')
        print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"),prompt)

        res = ai_api.ai_model(conf[0], conf[1], conf[2], sysprompt,prompt).chat_api()
        res = {"response": f'<span style="color: green;">{conf[0]}</span>'+'\n'+res}
        return JsonResponse(res, safe=False)

def chat_gpt4o(request):
    conf = env_conf.gpt4o
    if request.method == 'POST':
        data = json.loads(request.body)
        prompt = data.get('prompt')
        sysprompt = data.get('sysprompt')
        print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"),prompt)

        res = ai_api.ai_model(conf[0], conf[1], conf[2], sysprompt,prompt).chat_api()
        res = {"response": f'<span style="color: green;">{conf[0]}</span>'+'\n'+res}
        return JsonResponse(res, safe=False)

@csrf_exempt
def prompts(request):
    items = [
             '请认真思考确保准确，回答精简不必太啰嗦',
             '请认真思考确保准确，并且详细解释一下',
             '只需回答结果即可，无需解释',
             '列出几种不同的方案或者思路或者办法',
             '',
             ]
    return JsonResponse({'prompts': items})

if __name__ == '__main__':
    ai_api.ai_model(env_conf.gpt4o[0], env_conf.gpt4o[1], env_conf.gpt4o[2], '你叫小明','你是谁').chat_api()